public class demo2 {
    public static boolean isRunning = true;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (isRunning) {
                System.out.println("线程正在执行！");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程结束了");
        });
        t.start();
        System.out.println("3s后结束线程");
        Thread.sleep(1000);
        isRunning = false;
    }
}
